CC := gcc
AR := ar

IDIR := include
SRCDIR := lib
SRC := $(wildcard $(SRCDIR)/*.c)
ODIR := obj
OBJ := $(patsubst $(SRCDIR)/%.c, $(ODIR)/%.o, $(SRC))
FLEX := flex/in.l
FOUT := flex/flex.yy.c
FOBJ := obj/flex.yy.o

CFLAGS := -O2 -march=native -fomit-frame-pointer -finline-functions -pthread -Wall -I$(IDIR)
ARFLAGS := r
OUT := bin/libopp.a

all: setup $(FOBJ) $(OBJ)
	@$(AR) $(ARFLAGS) $(OUT) $(OBJ) $(FOBJ)

setup:
	@mkdir -pv obj bin

$(FOBJ): $(FOUT)
	$(CC) -c $< -o $@ $(CFLAGS)

$(FOUT): $(FLEX)
	flex -Pyy --header-file=flex/flex.yy.h -o $@ $<

$(ODIR)/%.o: $(SRCDIR)/%.c $(IDIR)/%.h
	$(CC) -c $< -o $@ $(CFLAGS) 

clean:
	@rm -f $(FOUT)
	@rm -f $(patsubst %.c, %.h, $(FOUT))
	@rm -f $(ODIR)/*.o
	@rm -f $(OUT)
